package com.atguigu.jdbc2;

import com.atguigu.jdbc.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/*
    对DBUtils再次封装
 */
public class BaseDAO<T> {

    private QueryRunner queryRunner = new QueryRunner();

    /*
        增，删，改
     */
    public int update(Connection connection,String sql,Object ... obj){
        try {
            return queryRunner.update(connection, sql, obj);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            //让程序停止
            throw new RuntimeException(throwables.getMessage());
        }
    }

    /*
        查询
     */
    public List<T> query(Connection connection,String sql,Class clazz,Object ... obj){
        try {
            List<T> query = queryRunner.query(connection, sql,
                    new BeanListHandler<T>(clazz), obj);
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            //让程序停止
            throw new RuntimeException(throwables.getMessage());
        }
    }

}
